package cn.xspace.wnx.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.xspace.android.xafcommon.base.BaseFragment;

import java.util.ArrayList;

import cn.xspace.wnx.R;
import cn.xspace.wnx.util.Navigator;
import cn.xspace.wnx.util.RProxy;
import cn.xspace.wnx.view.InterceptViewPager;

/**
 * Created by liuchen on 2016/10/8.
 */

public class WnxMainFragment extends BaseFragment {
    private static final String TAG = "WnxMainFragment";
    //    private PagerSlidingTabStrip mPagerSlidingTabStrip;
    private RadioGroup mRadioGroup;
    private RadioButton leftButton;
    private RadioButton rightButton;
    private InterceptViewPager mViewPager;
    private ArrayList<Fragment> fragments;
    private ImageView newChatView;
    private ImageView iconBack;


    public static WnxMainFragment newInstance() {

        Bundle args = new Bundle();
        WnxMainFragment fragment = new WnxMainFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View initView(LayoutInflater inflater) {
        Log.d(TAG, "initView");
        return inflater.inflate(R.layout.activity_wnx_main, null);
    }

    @Override
    protected void initFindViewById(View view) {
        mViewPager = (InterceptViewPager) view.findViewById(RProxy.pager);
        mRadioGroup = (RadioGroup) view.findViewById(R.id.radio_group);
        leftButton = (RadioButton) view.findViewById(R.id.radio_left);
        rightButton = (RadioButton) view.findViewById(R.id.radio_right);
        newChatView = (ImageView) view.findViewById(R.id.iv_chat_new);
        iconBack = (ImageView) view.findViewById(R.id.icon_back);
        if (!isShowBack) {
            iconBack.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    protected void initEvent() {
        Log.d(TAG, "initEvent");
        super.initEvent();
//        mPagerSlidingTabStrip.setViewPager(mViewPager);
        mViewPager.setPagingEnabled(false);
        mViewPager.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                return false;
            }
        });
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.radio_left) {
                    mViewPager.setCurrentItem(0);
                } else if (checkedId == R.id.radio_right) {
                    mViewPager.setCurrentItem(1);
                }
            }
        });
        newChatView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Navigator.startNewChatActivity(mContext, null);
//                Navigator.startWeiNongJH(mContext);
            }
        });
        iconBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                exitActivity();
            }
        });
    }

    @Override
    public void initData() {
        Log.d(TAG, "initData");
        addFragment();
        String[] TITLES = getResources().getStringArray(RProxy.tab_main_wnx);
        mViewPager.setAdapter(new MainPageAdapter(getActivity().getSupportFragmentManager(), fragments));
        leftButton.setText(TITLES[0]);
        rightButton.setText(TITLES[1]);
    }

    private void addFragment() {
        fragments = new ArrayList<Fragment>();
        fragments.add(MessageFragment.newInstance());
        fragments.add(AddressBookFragment.newInstance(null));
    }


    public class MainPageAdapter extends FragmentPagerAdapter {
        String[] TITLES = getResources().getStringArray(RProxy.tab_main_wnx);
        private ArrayList<Fragment> fragmentsList;

        public MainPageAdapter(FragmentManager fm) {
            super(fm);
        }

        public MainPageAdapter(FragmentManager fm, ArrayList<Fragment> fragmentsList) {
            super(fm);
            this.fragmentsList = fragmentsList;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return TITLES[position];
        }

        @Override
        public int getCount() {
            return TITLES.length;
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentsList.get(position);
        }
    }

}
